package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap 转换department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString,Department.class);
        //根据医院编号和科室编号查询
        Department departmentExist = departmentRepository.
                getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //判断
        if(departmentExist!=null) {
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(departmentExist.getIsDeleted());
            department.setId(departmentExist.getId());
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //分页实现
    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        //创建实例
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }
    //根据医院编号查询所有科室信息
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example=Example.of(department);
        List<Department> all = departmentRepository.findAll(example);
        //组装出前端所需要的数据
        //Key:大科室编号，value：当前大科室底下的所有子科室列表
        Map<String, List<Department>> map = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        List<DepartmentVo> bigDeptList=new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : map.entrySet()) {
            DepartmentVo big = new DepartmentVo();
            String bigcode = entry.getKey();
            big.setDepcode(bigcode);
            List<Department> smallDeptList = entry.getValue();
            big.setDepname(smallDeptList.get(0).getBigname());
            //封装当前大科室对应的所有子科室信息
            List<DepartmentVo> children=new ArrayList<>();
            for (Department department1 : smallDeptList) {
                DepartmentVo small= new DepartmentVo();
                String depcode = department1.getDepcode();
                String depname = department1.getDepname();
                small.setDepcode(depcode);
                small.setDepname(depname);
                children.add(small);
            }
            big.setChildren(children);
            bigDeptList.add(big);
        }
        return bigDeptList;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }

    //删除科室方法
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(null != department) {
            departmentRepository.deleteById(department.getId());
        }
    }


}